package com.dream21th.admin.controller;


import com.dream21th.admin.common.dto.MenuPermissionDTO;
import com.dream21th.admin.entity.SysMenuEntity;
import com.dream21th.admin.entity.SysUserEntity;
import com.dream21th.admin.service.ShiroService;
import com.dream21th.admin.service.SysMenuService;
import com.dream21th.admin.service.SysUserService;
import com.dream21th.admin.utils.Constant;
import com.dream21th.common.ComResp;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/sys/menu")
public class SysMenuController{
	@Autowired
	private SysMenuService sysMenuService;

	@Autowired
    private ShiroService shiroService;

	@Autowired
	private SysUserService sysUserService;
	/**
	 * 导航菜单
	 */
	@PostMapping("/nav")
	public ComResp<MenuPermissionDTO> nav(Authentication authentication){
		String username = (String)authentication.getPrincipal();
		SysUserEntity sysUserEntity = sysUserService.queryByUserName(username);
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(sysUserEntity.getUserId());
		Set<String> permissions = shiroService.getUserPermissions(sysUserEntity.getUserId());
		return new ComResp<MenuPermissionDTO>().data(MenuPermissionDTO.builder().menuList(menuList).permissions(permissions).build()).success();
	}

	/**
	 * 所有菜单列表
	 */
	@PostMapping("/list")
	public ComResp<List<SysMenuEntity>> list(){
		List<SysMenuEntity> menuList = sysMenuService.list();
		for(SysMenuEntity sysMenuEntity : menuList){
			SysMenuEntity parentMenuEntity = sysMenuService.getById(sysMenuEntity.getParentId());
			if(parentMenuEntity != null){
				sysMenuEntity.setParentName(parentMenuEntity.getName());
			}
		}
		return new ComResp<List<SysMenuEntity>>().data(menuList).success();
	}

	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@PostMapping("/select")
	public ComResp select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();

		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);

		return new ComResp().data(menuList).success();
	}

	/**
	 * 菜单信息
	 */
	@PostMapping("/infoById")
	public ComResp info(@RequestBody SysMenuEntity sysMenuEntity){
		SysMenuEntity menu = sysMenuService.getById(sysMenuEntity.getMenuId());
		return new ComResp().data(menu).success();
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	public ComResp save(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);

		sysMenuService.save(menu);

		return new ComResp().success();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	public ComResp update(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);

		sysMenuService.updateById(menu);

		return new ComResp().success();
	}

	/**
	 * 删除
	 */
	@PostMapping("/deleteById")
	public ComResp delete(@RequestBody SysMenuEntity sysMenuEntity){
		Long menuId=sysMenuEntity.getMenuId();
		if(menuId <= 26){
			return new ComResp().result("500","系统菜单，不能删除");
		}

		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			return new ComResp().result("500","请先删除子菜单或按钮");
		}

		sysMenuService.delete(menuId);

		return new ComResp().success();
	}

	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new RuntimeException("菜单名称不能为空");
		}

		if(menu.getParentId() == null){
			throw new RuntimeException("上级菜单不能为空");
		}

		//菜单
		if(menu.getType() == Constant.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new RuntimeException("菜单URL不能为空");
			}
		}

		//上级菜单类型
		int parentType = Constant.MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
			parentType = parentMenu.getType();
		}

		//目录、菜单
		if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
				menu.getType() == Constant.MenuType.MENU.getValue()){
			if(parentType != Constant.MenuType.CATALOG.getValue()){
				throw new RuntimeException("上级菜单只能为目录类型");
			}
			return ;
		}

		//按钮
		if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
			if(parentType != Constant.MenuType.MENU.getValue()){
				throw new RuntimeException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
